package 字符串;

import java.util.Scanner;

public class 字符串匹配 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.next();
        int flag = scanner.nextInt();
        int n = scanner.nextInt();
        String[] strs = new String[n];
        for (int i = 0; i < strs.length; i++) {
            strs[i] = scanner.next();
        }
        for (int i = 0; i < strs.length; i++) {
            if (flag==0){
                if (strs[i].toLowerCase().indexOf(str.toLowerCase())>=0){
                    System.out.println(strs[i]);
                }
            }else {
                if (strs[i].indexOf(str)>=0){
                    System.out.println(strs[i]);
                }
            }
        }
    }
}
